package znbkTestPaperCtfd.save;

import android.app.Activity;
import android.content.DialogInterface;
import android.os.Handler;
import android.util.Log;

import com.example.lancooznbk.view.base.ILoadPaperView;
import com.lancoo.reviewsystem.utils.ToastUtils;
import com.lancoo.rxretrofitlibrary.http.HttpManager;
import com.lancoo.rxretrofitlibrary.listener.HttpOnNextListener;

import znbkChangeZsdLearned.ZnbkSaveLearnedZsdJsonModel;
import znbkListXtfx.ISystemReviewPresenter;
import znbkTestPaperCtfd.ZnbkPaperCtfdInfor;
import znbkTestPaperCtfd.activity.WrongTopicTutoringPaperActivity;

/**
 * Created by Administrator on 2018\3\29 0029.
 */

public class SaveWrongTopicTutoringPresenter implements ISystemReviewPresenter {

    private ILoadPaperView view;
    private int ModelFrom;
    private boolean isFromExit = false;
    private boolean isAssess = false;
    private boolean isSlide = false;

    public SaveWrongTopicTutoringPresenter(ILoadPaperView view, int modelFrom) {
        this.view = (ILoadPaperView) view;
        this.ModelFrom = modelFrom;
    }

    @Override
    public void getData(boolean isShowLoading) {
        //  if (isShowLoading) view.showLoading();
        HttpOnNextListener<String> httpOnNextListener = new HttpOnNextListener<String>() {

            @Override
            public void onNext(String s) {

                if(s.equals("1")){

                    if (isFromExit){
//                        ToastUtils.showShort("提交成功");
                        Log.e("==isFromExit==","======"+isFromExit);
                        view.getLoadingDialog().setOnDismissListener(new DialogInterface.OnDismissListener() {
                            @Override
                            public void onDismiss(DialogInterface dialog) {
                                if(isAssess){
                                    view.openAssessDialog();
                                }else {
                                    view.openExamReportDialog();
                                }
                            }
                        });
                        view.updatingResult(true);

                    }else{
                        if(!isSlide) {
//                            ToastUtils.showShort("保存成功");
                            new Handler().postDelayed(new Runnable() {
                                @Override
                                public void run() {
                                    ((Activity) view).finish();
                                }
                            }, 200);

                        }

                    }
                }else{
                    view.updatingResult(false);
                    ToastUtils.showShort("提交失败! 错误码"+s.toString());
                }

            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                //view.showNetError();
                ToastUtils.showShort("提交失败!");
            }
        };

        SaveWrongTopicTutoringApi api = new SaveWrongTopicTutoringApi(httpOnNextListener, (WrongTopicTutoringPaperActivity)view);

        api.setmSaveWrongTopicTutoringEntity(SaveWrongTopicTutoringModel.getSaveWrongTopicTutoringEntity(ZnbkPaperCtfdInfor.RecQuesForSaveTreeBean));

        HttpManager httpManager = HttpManager.getInstance();
        httpManager.doHttpDeal(api);
    }

    @Override
    public void getMoreData() {
        getData(false);
    }

    @Override
    public void getDataByStatus(int position) {
        getData(false);
    }

    public void getData(boolean isShowing ,boolean isFromExit,boolean isAssess,boolean isSlide){
        this.isFromExit = isFromExit;
        this.isAssess = isAssess;
        this.isSlide = isSlide;
        getData(isShowing);
    }

}